package com.ncr.travel.appmgr.client.view;

import java.util.Arrays;
import java.util.List;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseWheelEvent;
import com.google.gwt.event.dom.client.MouseWheelHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBoxBase;
import com.google.gwt.user.client.ui.ValueBoxBase;
import com.google.gwt.user.client.ui.Widget;
import com.ncr.travel.appmgr.client.event.PreviewConfigChangeEvent;
import com.ncr.travel.appmgr.client.view.TextWidget.TextAppMgrWindowUiBinder;
import com.ncr.travel.appmgr.model.shared.Alignment;
import com.ncr.travel.appmgr.style.client.AppManagerStyleClientBundle;

public class FontOptionsWidget extends Composite implements FontOptionsView {


    @UiTemplate("FontOptionsView.ui.xml")
    interface FontOptionsViewUiBinder extends UiBinder<Widget, FontOptionsWidget> {
        
    }
    private static FontOptionsViewUiBinder fontOptionsViewUiBinder = GWT.create(FontOptionsViewUiBinder.class);
	
    private final FlowPanel mainPanel;
	@UiField
    HorizontalPanel fontsMainPanel;
	@UiField
	ListBox stylesLbox;
	@UiField
	ListBox sizeLbox;
	@UiField
	Image leftAlignImg;
	@UiField
	Image centerAlignImg;
	@UiField
	Image justifyAlignImg;
	@UiField
	Image rightAlignImg;
	
	@UiField(provided=true)
	final ColorPallete pallete;

	private TextBoxBase associatedWidget;

	private Alignment alignmentSelected;
	private String colorSelected;
	private int styleSelectedIndex;
	private int sizeSelectedIndex;
	private final ClickHandler fontOptionsClickHandler;
	private final ChangeHandler textConfigChangeHandler;

	public static final List<String> FONT_STYLES = Arrays.asList("Styles", "Default", "Bold", "Italic", "Underline");
	public static final List<String> FONT_SIZES = Arrays.asList("Size", "10", "11", "12", "14");
	

	public FontOptionsWidget(FlowPanel mainPanel, ClickHandler clickHandler, ChangeHandler changeHandler) {
		System.out.println("Constructing FontOptionsWidget");
	    this.mainPanel = mainPanel;
        this.fontOptionsClickHandler = clickHandler;
        this.textConfigChangeHandler = changeHandler;
        /*This Color pallete is provided to UiBinder*/
        this.pallete = new ColorPallete(mainPanel, clickHandler);
        this.initWidget(fontOptionsViewUiBinder.createAndBindUi(this));
        createFontOptions();

	}

	@Override
	public ListBox getFontStylesListBox() {
		return stylesLbox;
	}

	@Override
	public ListBox getFontSizesListBox() {
		return sizeLbox;
	}

	@Override
	public void setSelectedStyle(int index) {
		styleSelectedIndex = index;
		stylesLbox.setSelectedIndex(index);
	}

	@Override
	public String getSelectedStyle() {
		return stylesLbox.getValue(styleSelectedIndex);
	}

	@Override
	public int getSelectedSize() {
		return Integer.valueOf(sizeLbox.getValue(sizeSelectedIndex));
	}

	@Override
	public void setSelectedSize(int index) {
		sizeSelectedIndex = index;
		sizeLbox.setSelectedIndex(index);
	}

	@Override
	public void setSelectedColor(String color) {
		colorSelected = color;
		pallete.setSelectedColor(color);
	}

	private void removeBorderForUnSelected() {
		leftAlignImg.removeStyleName("borderForSelected");
		centerAlignImg.removeStyleName("borderForSelected");
		justifyAlignImg.removeStyleName("borderForSelected");
		rightAlignImg.removeStyleName("borderForSelected");
	}

	@Override
	public void setSelectedAlignment(Alignment alignment) {
		removeBorderForUnSelected();
		switch (alignment) {
		case LEFT: {
			alignmentSelected = Alignment.LEFT;
			leftAlignImg.addStyleName("borderForSelected");
			break;
		}
		case RIGHT: {
			alignmentSelected = Alignment.RIGHT;
			rightAlignImg.addStyleName("borderForSelected");
			break;
		}
		case CENTER: {
			alignmentSelected = Alignment.CENTER;
			centerAlignImg.addStyleName("borderForSelected");
			break;
		}
		case JUSTIFY: {
			alignmentSelected = Alignment.JUSTIFY;
			justifyAlignImg.addStyleName("borderForSelected");
			break;
		}
		}
	}

	@Override
	public Alignment getAlignmentSelected() {
		return alignmentSelected;
	}

	@Override
	public void setAlignmentSelected(Alignment alignmentSelected) {
		this.alignmentSelected = alignmentSelected;
	}

	@Override
	public ValueBoxBase<String> getAssociatedWidget() {
		return associatedWidget;
	}

	@Override
	public void setAssociatedWidget(ValueBoxBase<String> associatedWidget) {
		this.associatedWidget = (TextBoxBase) associatedWidget;
	}

	@Override
	public String getColorSelected() {
		return colorSelected;
	}

	@Override
	public void setColorSelected(String colorSelected) {
		this.colorSelected = colorSelected;
	}

	
	public void createFontOptions() {

		stylesLbox.addChangeHandler(textConfigChangeHandler);

		for (String style : FONT_STYLES) {
			stylesLbox.addItem(style);
		}

		sizeLbox.addChangeHandler(textConfigChangeHandler);
		for (String size : FONT_SIZES) {
			sizeLbox.addItem(size);
		}

		leftAlignImg.getElement().setId(Alignment.LEFT.name());

		leftAlignImg.addClickHandler(fontOptionsClickHandler);

		centerAlignImg.getElement().setId(Alignment.CENTER.name());

		centerAlignImg.addClickHandler(fontOptionsClickHandler);

		justifyAlignImg.getElement().setId(Alignment.JUSTIFY.name());

		justifyAlignImg.addClickHandler(fontOptionsClickHandler);

		rightAlignImg.getElement().setId(Alignment.RIGHT.name());

		rightAlignImg.addClickHandler(fontOptionsClickHandler);		
		
	}
}
